package com.saclub.app.fragment;

import android.app.Activity;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.saclub.app.R;
import com.saclub.app.activity.MyToolBarActivity;
import com.saclub.app.bean.part.User;
import com.saclub.app.common.AccessTokenKeeper;
import com.saclub.app.common.MyPageHelper;
import com.saclub.app.http.ApiRequest;

public class AccountEditFragment extends BaseProgressFragment<MyToolBarActivity> {

	@Bind(R.id.id_user_login_mail)
	protected TextView mailText;

	@Bind(R.id.id_user_login_pwd)
	protected TextView pwdText;

	@Bind(R.id.id_user_login_phone)
	protected TextView phoneText;

	@Bind(R.id.id_user_login_name)
	protected TextView nameText;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_account_edit;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {
	}

	@Override
	protected void initData() {

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {

			User user = tokenKeeper.getUser();

			mailText.setText(user.getEmail());
			pwdText.setText(user.getPwd());
			phoneText.setText(user.getPhone());
			nameText.setText(user.getName());

		}

	}

	@OnClick(R.id.id_user_btn_save)
	public void btnSave() {
		
		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {
			
			String mail = mailText.getText().toString();
			String pwd = pwdText.getText().toString();
			String phone = phoneText.getText().toString();
			String name = nameText.getText().toString();

			RequestParams params = new RequestParams();
			params.put("email", mail);
			params.put("name", name);
			params.put("phone", phone);
			params.put("pass", pwd);
			params.put("webuser_id", tokenKeeper.getOpenId());

			ApiRequest.userUpdate.request(this, params);
			
		}
		
	}

	public static void go(Activity act) {
		MyPageHelper.accountEdit.showMyPage(act);
	}
}
